{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas import DataFrame,Series\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "    Python  Java  PHP\n张飞     100    80   60\n吕布      90    70   50\n关羽      80    60   40",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>PHP</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>张飞</th>\n      <td>100</td>\n      <td>80</td>\n      <td>60</td>\n    </tr>\n    <tr>\n      <th>吕布</th>\n      <td>90</td>\n      <td>70</td>\n      <td>50</td>\n    </tr>\n    <tr>\n      <th>关羽</th>\n      <td>80</td>\n      <td>60</td>\n      <td>40</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = DataFrame(\n",
    "    data={\n",
    "      'Python': [100, 90, 80],\n",
    "      'Java': [80, 70, 60],\n",
    "      'PHP': [60, 50, 40]\n",
    "    },\n",
    "    index=['张飞', \"吕布\", '关羽']\n",
    ")\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "    Python  Java  PHP  Go\n张飞     100    80   60  40\n吕布      90    70   50  30\n关羽      80    60   40  20\n刘备      70    50   30  10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>PHP</th>\n      <th>Go</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>张飞</th>\n      <td>100</td>\n      <td>80</td>\n      <td>60</td>\n      <td>40</td>\n    </tr>\n    <tr>\n      <th>吕布</th>\n      <td>90</td>\n      <td>70</td>\n      <td>50</td>\n      <td>30</td>\n    </tr>\n    <tr>\n      <th>关羽</th>\n      <td>80</td>\n      <td>60</td>\n      <td>40</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>刘备</th>\n      <td>70</td>\n      <td>50</td>\n      <td>30</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = DataFrame(\n",
    "    data={\n",
    "      'Python': [100, 90, 80, 70],\n",
    "      'Java': [80, 70, 60, 50],\n",
    "      'PHP': [60, 50, 40, 30],\n",
    "      'Go': [40, 30, 20, 10]\n",
    "    },\n",
    "    index=['张飞', \"吕布\", '关羽', \"刘备\"]\n",
    ")\n",
    "df2\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "    Go   Java    PHP  Python\n关羽 NaN  120.0   80.0   160.0\n刘备 NaN    NaN    NaN     NaN\n吕布 NaN  140.0  100.0   180.0\n张飞 NaN  160.0  120.0   200.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Go</th>\n      <th>Java</th>\n      <th>PHP</th>\n      <th>Python</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>关羽</th>\n      <td>NaN</td>\n      <td>120.0</td>\n      <td>80.0</td>\n      <td>160.0</td>\n    </tr>\n    <tr>\n      <th>刘备</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>吕布</th>\n      <td>NaN</td>\n      <td>140.0</td>\n      <td>100.0</td>\n      <td>180.0</td>\n    </tr>\n    <tr>\n      <th>张飞</th>\n      <td>NaN</td>\n      <td>160.0</td>\n      <td>120.0</td>\n      <td>200.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据索引计算\n",
    "df1+df2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "      Go   Java    PHP  Python\n关羽  20.0  120.0   80.0   160.0\n刘备  10.0   50.0   30.0    70.0\n吕布  30.0  140.0  100.0   180.0\n张飞  40.0  160.0  120.0   200.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Go</th>\n      <th>Java</th>\n      <th>PHP</th>\n      <th>Python</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>关羽</th>\n      <td>20.0</td>\n      <td>120.0</td>\n      <td>80.0</td>\n      <td>160.0</td>\n    </tr>\n    <tr>\n      <th>刘备</th>\n      <td>10.0</td>\n      <td>50.0</td>\n      <td>30.0</td>\n      <td>70.0</td>\n    </tr>\n    <tr>\n      <th>吕布</th>\n      <td>30.0</td>\n      <td>140.0</td>\n      <td>100.0</td>\n      <td>180.0</td>\n    </tr>\n    <tr>\n      <th>张飞</th>\n      <td>40.0</td>\n      <td>160.0</td>\n      <td>120.0</td>\n      <td>200.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Nan用0\n",
    "df1.add(df2,fill_value=0)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "    Go  Java  PHP  Python\n关羽 NaN   0.0  0.0     0.0\n刘备 NaN   NaN  NaN     NaN\n吕布 NaN   0.0  0.0     0.0\n张飞 NaN   0.0  0.0     0.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Go</th>\n      <th>Java</th>\n      <th>PHP</th>\n      <th>Python</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>关羽</th>\n      <td>NaN</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>刘备</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>吕布</th>\n      <td>NaN</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>张飞</th>\n      <td>NaN</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1-df2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "    Python  Java  PHP\n张飞     100    80   60\n吕布      90    70   50\n关羽      80    60   40",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>PHP</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>张飞</th>\n      <td>100</td>\n      <td>80</td>\n      <td>60</td>\n    </tr>\n    <tr>\n      <th>吕布</th>\n      <td>90</td>\n      <td>70</td>\n      <td>50</td>\n    </tr>\n    <tr>\n      <th>关羽</th>\n      <td>80</td>\n      <td>60</td>\n      <td>40</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "Python    100\nJava       10\nPHP         1\ndtype: int64"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Series与DataFrame之间的运算\n",
    "df1 = DataFrame(\n",
    "    data={\n",
    "      'Python': [100, 90, 80],\n",
    "      'Java': [80, 70, 60],\n",
    "      'PHP': [60, 50, 40]\n",
    "    },\n",
    "    index=['张飞', \"吕布\", '关羽']\n",
    ")\n",
    "\n",
    "s = Series([100, 10, 1], index=df1.columns)\n",
    "display(df1,s)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "    Python  Java  PHP\n张飞     200    90   61\n吕布     190    80   51\n关羽     180    70   41",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>PHP</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>张飞</th>\n      <td>200</td>\n      <td>90</td>\n      <td>61</td>\n    </tr>\n    <tr>\n      <th>吕布</th>\n      <td>190</td>\n      <td>80</td>\n      <td>51</td>\n    </tr>\n    <tr>\n      <th>关羽</th>\n      <td>180</td>\n      <td>70</td>\n      <td>41</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1+s"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "        Python  Java  PHP\nJava       NaN   NaN  NaN\nPHP        NaN   NaN  NaN\nPython     NaN   NaN  NaN\n关羽         NaN   NaN  NaN\n吕布         NaN   NaN  NaN\n张飞         NaN   NaN  NaN",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>PHP</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>Java</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>PHP</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>Python</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>关羽</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>吕布</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>张飞</th>\n      <td>NaN</td>\n      <td>NaN</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据列索引加\n",
    "# df1.add(s,axis=1)\n",
    "# df1.add(s,axis=0)\n",
    "\n",
    "# df1.add(s,axis=\"columns\")\n",
    "# df1.add(s,axis=\"index\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}